package cn.jinjimi.app.custom.ui.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import cn.jinjimi.app.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by libilang on 15/11/27.
 */
public class EditCustomInfoDialog extends Dialog {

    public List<String> info;
    public DIALOGTYPE mCurrentType;
    public ListView mListView;
    public Context mContext = null;
    public dialogInfoListener listener;

    /* 弹窗的类型 */
    public enum DIALOGTYPE {
        SELECT_CUSTOM_LEVEL,//选择客户层级
        SELECT_CUSTOM_STATUS,//选择客户状态
        SELECT_CUSTOM_GENDER,//选择客户性别
        SELECT_CUSTOM_IDCODE// 选择客户身份证类型
    }

    public EditCustomInfoDialog(Context context, DIALOGTYPE type, dialogInfoListener listener) {

        super(context, R.style.dialog_parent_style);
        this.mContext = context;
        this.mCurrentType = type;
        this.listener = listener;
        setContentView(R.layout.dialog_edit_custominfo);
        getWindow().setGravity(Gravity.CENTER);
        setCanceledOnTouchOutside(true);

        initView();
    }

    private void initView() {
        mListView = (ListView) findViewById(R.id.listview_info);
        info = new ArrayList<>();
        if (mCurrentType == DIALOGTYPE.SELECT_CUSTOM_LEVEL) {
            info.add(mContext.getString(R.string.custom_A));
            info.add(mContext.getString(R.string.custom_B));
            info.add(mContext.getString(R.string.custom_C));
            info.add(mContext.getString(R.string.custom_D));
            info.add(mContext.getString(R.string.hasno));
        } else if (mCurrentType == DIALOGTYPE.SELECT_CUSTOM_STATUS) {
            info.add(mContext.getString(R.string.customer_unsold_text));
            info.add(mContext.getString(R.string.custom_already_written));
            info.add(mContext.getString(R.string.hasno));
        } else if (mCurrentType == DIALOGTYPE.SELECT_CUSTOM_GENDER) {
            info.add(mContext.getString(R.string.custom_gender_man));
            info.add(mContext.getString(R.string.custom_gender_woman));
        } else if (mCurrentType == DIALOGTYPE.SELECT_CUSTOM_IDCODE) {
            info.add(mContext.getString(R.string.custom_idcode_id));
            info.add(mContext.getString(R.string.custom_idcode_passport));
            info.add(mContext.getString(R.string.hasno));
        }

        mListView.setAdapter(new InfoAdapter());
    }

    @Override
    public void dismiss() {
        super.dismiss();
        mContext = null;
    }

    class InfoAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return info.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final Holder holder;
            if (convertView == null) {
                holder = new Holder();
                convertView = LayoutInflater.from(mContext).inflate(R.layout.item_infos, null);
                holder.textView = (TextView) convertView.findViewById(R.id.check_tag);
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }
            holder.textView.setText(info.get(position));
            holder.textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.Success(position);
                    dismiss();
                }
            });

            return convertView;
        }

    }

    public class Holder {
        public TextView textView;
    }

    public interface dialogInfoListener {
        void Success(int flag);
    }

}